package com.zjl.学习.第05章_作业控制.B_作业的停止;

import com.zjl.springbatch.util.OutColour;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.nio.charset.Charset;

/**
 * ●一种自然结束
 * 作业成功执行，正常停止，此时作业返回状态为：COMPLETED
 * ●一种异常结束
 * 作业执行过程因为各种意外导致作业中断而停止，大多数作业返回状态为：FAILED
 * ●一种编程结束
 * 某个步骤处理数据结果不满足下一步骤执行前提，手动让其停止，一般设置返回状态为：STOPED
 *
 */
@SpringBootApplication
@EnableBatchProcessing//开启spring batch
public class B_作业的停止 {
    public static void main(String[] args) {
//        args = new String[]{"name=张蛟龙3"};//模拟 入参
        System.setProperty("spring.profiles.active","01");//  同理 --spring.profiles.active=xxx

        SpringApplication.run(B_作业的停止.class, args);
        System.out.println("***********启动完毕**当前编码:" + Charset.defaultCharset().name() + "****************");

    }

    @Autowired//@EnableBatchProcessing 这个注解帮你从 spring batch 的配置文件中加载了
    private JobLauncher jobLauncher;//Job 启动器
    @Autowired//Job 构造工厂
    private JobBuilderFactory jobBuilderFactory;
    @Autowired//构建 Step
    private StepBuilderFactory stepBuilderFactory;
    @Bean//任务 Tasklet 这是最简单的    还有Chunk块处理
    public Tasklet tasklet05A(){
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                ExecutionContext executionContext = chunkContext.getStepContext().getStepExecution().getExecutionContext();
                executionContext.put("xx","xx");//放入shep的共享变量

                chunkContext.getStepContext().getStepExecution().setTerminateOnly();//设置 此操作状态为   STOPPED  //表示已完成的加工 中断状态停止
                OutColour.out.printlnRed("****************A*******************");
                return RepeatStatus.FINISHED;//已完成
            }
        };
    }
    @Bean
    public Tasklet tasklet05B(){
        return new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
                OutColour.out.printlnRed("****************B*******************");
                return RepeatStatus.FINISHED;//已完成
            }
        };
    }

    @Bean
    public Step step05A() throws Exception {
        return stepBuilderFactory.get("step05A")
                .tasklet(tasklet05A())
//                .listener(myStepExecutionListener05)//使用自定义 步骤监听器 实现 STOPPED
//                .allowStartIfComplete(true)//允许此步骤可重复执行//表示无论如何，都会执行此步骤
                .build();
    }
    @Bean
    public Step step05B() throws Exception {
        //将 B1 B2 B3  都放入 B 里
        return stepBuilderFactory.get("step05B").tasklet(tasklet05B()).build();
    }
    @Autowired//构建 Step
    private MyStepExecutionListener05 myStepExecutionListener05;
    @Bean// 会自己找 指定符合的类传进去入参
    public Job job005ZYTZ() throws Exception {
        return jobBuilderFactory.get("job005作业的停止1")
                .start(step05A()).on(ExitStatus.STOPPED.getExitCode()).stopAndRestart(step05A())
                .from(step05A()).on("*").to(step05B())
                .end()
                .build();

    }
}
